package com.cpi.cornpp.purchaseorder.action;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.purchase.program.exception.PurchaseProgramException;
import com.cpi.cornpp.purchaseorder.service.PurchaseOrderVersionService;
import com.cs.common.utils.NumberUtil;

public class DeleteBalancePurchaseOrderVersionAction extends Action {
	private static final String DEFAULT_FORWARD = "default";//$NON-NLS-1$
	private static final String PURCHASE_ORDER_VERSION_ID = "purchaseOrderVersionId";//$NON-NLS-1$
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private static final String SUCCESS_DELETE_LABEL = "common.delete.success";//$NON-NLS-1$
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws PurchaseProgramException {
		HttpSession httpSession = null;
		PurchaseOrderVersionService purchaseOrderVersionService = null;
		int purchaseOrderVersionId = 0;
		String messageError = null;
		MessageResources messageResources = null;
		try{
			httpSession = request.getSession();
			messageResources = getResources(request);
			purchaseOrderVersionService = new PurchaseOrderVersionService();
			purchaseOrderVersionId = NumberUtil.getInt(request.getParameter(PURCHASE_ORDER_VERSION_ID));
			purchaseOrderVersionService.deleteBalancesVersion(purchaseOrderVersionId);
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_DELETE_LABEL);
			httpSession.setAttribute(MESSAGE, messageError);
		}catch (Exception exception) {
			exception.printStackTrace(System.out);
			PurchaseProgramException purchaseProgramException = null;
			purchaseProgramException = new PurchaseProgramException(exception, CornPPException.LAYER_ACTION, CornPPException.ACTION_DELETE);
			httpSession.setAttribute(CUSTOM_EXCEPTION, purchaseProgramException);
			throw purchaseProgramException;
		}
		return mapping.findForward(DEFAULT_FORWARD);
	}
}
